﻿package core.manager
{
    import __AS3__.vec.*;
    import core.enum.*;
    import core.image.*;
    import core.resource.*;
    import core.ui.controls.*;
    import core.ui.dialogs.*;
    import core.ui.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.net.*;
    import flash.printing.*;
    import flash.utils.*;

    public class CommandManager extends Object
    {
        private var f:Filter;
        private var p:Tuyitu;

        public function CommandManager(param1:Tuyitu)
        {
            this.p = param1;
            this.f = new Filter();
            return;
        }// end function

        public function CheckCanvas() : Boolean
        {
            return Boolean(this.p.canvas != null && this.p.canvas.document != null);
        }// end function

        public function CheckBitmapLayer() : Boolean
        {
            return Boolean(this.CheckCanvas() && this.p.canvas.document.GetLayer().type == LayerType.Bitmap);
        }// end function

        public function CheckValidBitmapLayer() : Boolean
        {
            return Boolean(this.CheckCanvas() && this.p.canvas.document.GetLayer().type == LayerType.Bitmap && this.p.canvas.document.GetLayer().hasBitmap);
        }// end function

        public function CheckNotLockedLayer() : Boolean
        {
            return Boolean(this.p.canvas.document.anr != 0 || this.p.canvas.document.transparent);
        }// end function

        public function NewImage() : void
        {
            new NewImageDialog(this.p);
            return;
        }// end function

        public function OpenImage(type:Boolean = false) : void
        {
            new OpenImageDialog(this.p, "", "", false, type);
            return;
        }// end function

        public function OpenImageFrom(type:Boolean = false) : void
        {
            new OpenImageFromDialog(this.p, type);
            return;
        }// end function

        public function OpenImageLibrary(type:Boolean = false) : void
        {
            var aslayer:Boolean;
            var _arg1:* = type;
            aslayer = _arg1;
            if (this.p.user != null)
            {
                new ImageLibraryWindow(this.p, aslayer);
            }
            else
            {
                new LoginDialog(this.p, function ()
            {
                OpenImageLibrary(aslayer);
                return;
            }// end function
            );
            }
            return;
        }// end function

        public function SaveImage() : void
        {
            if (this.CheckCanvas())
            {
                new SaveImageWindow(this.p);
            }
            return;
        }// end function

        public function CloseImage() : void
        {
            if (this.CheckCanvas())
            {
                this.p.RemoveCanvas();
            }
            return;
        }// end function

        public function ConfirmClose(... args) : void
        {
            args = new activation;
            var _args:* = args;
            var args:* = ;
            if (this.CheckCanvas())
            {
                if (this.p.canvas.history.IsEmpty)
                {
                    this.p.RemoveCanvas();
                }
                else
                {
                    new ConfirmDialog(this.p, "rm_save_before_close", function ()
            {
                new SaveImageWindow(p, true);
                return;
            }// end function
            , this.p.RemoveCanvas);
                }
            }
            return;
        }// end function

        public function Login(... args) : void
        {
            new LoginDialog(this.p);
            return;
        }// end function

        public function Register(... args) : void
        {
            new RegisterDialog(this.p);
            return;
        }// end function

        public function LostPassword() : void
        {
            new LostPasswordDialog(this.p);
            return;
        }// end function

        public function PrintImage() : void
        {
            var _loc_1:PrintJob = null;
            var _loc_2:PrintJobOptions = null;
            var _loc_3:BitmapData = null;
            var _loc_4:Sprite = null;
            if (this.CheckCanvas())
            {
                _loc_1 = new PrintJob();
                _loc_2 = new PrintJobOptions();
                _loc_2.printAsBitmap = true;
                _loc_3 = new BitmapData(this.p.canvas.document.dw, this.p.canvas.document.dh, true, 4294967295);
                _loc_3.draw(this.p.canvas.document);
                _loc_4 = new Sprite();
                _loc_4.x = -3500;
                _loc_4.y = -3500;
                _loc_4.addChild(new Bitmap(_loc_3));
                this.p.addChild(_loc_4);
                if (_loc_1.start())
                {
                    if (_loc_1.pageHeight < _loc_4.height || _loc_1.pageWidth < _loc_4.width)
                    {
                        if (_loc_4.height / _loc_1.pageHeight > _loc_4.width / _loc_1.pageWidth)
                        {
                            var _loc_5:* = _loc_1.pageHeight / _loc_4.height;
                            _loc_4.scaleY = _loc_1.pageHeight / _loc_4.height;
                            _loc_4.scaleX = _loc_5;
                        }
                        else
                        {
                            var _loc_5:* = _loc_1.pageWidth / _loc_4.width;
                            _loc_4.scaleY = _loc_1.pageWidth / _loc_4.width;
                            _loc_4.scaleX = _loc_5;
                        }
                    }
                    _loc_1.addPage(_loc_4, null, _loc_2);
                    _loc_1.send();
                }
                this.p.removeChild(_loc_4);
                _loc_4 = null;
            }
            return;
        }// end function

        public function Undo() : void
        {
            if (this.CheckCanvas())
            {
                if (this.p.canvas.history.Undo())
                {
                    this.p.canvas.Message("rm_undo");
                }
            }
            return;
        }// end function

        public function Redo() : void
        {
            if (this.CheckCanvas())
            {
                if (this.p.canvas.history.Redo())
                {
                    this.p.canvas.Message("rm_redo");
                }
            }
            return;
        }// end function

        public function Cut() : void
        {
            var _loc_1:BitmapData = null;
            if (this.CheckCanvas())
            {
                _loc_1 = this.Slice(true);
                if (_loc_1)
                {
                    this.p.clipboard.bitmap = _loc_1;
                }
            }
            return;
        }// end function

        public function Copy() : void
        {
            var _loc_1:BitmapData = null;
            if (this.CheckCanvas())
            {
                _loc_1 = this.Slice(false);
                if (_loc_1)
                {
                    this.p.clipboard.bitmap = _loc_1;
                    this.p.canvas.Message("rm_copy");
                }
            }
            return;
        }// end function

        public function PasteGlobal(type:String) : void
        {
            var ldr:*;
            var _arg1:* = type;
            var data:*;
            ldr;
            var result:* = _arg1;
            if (result.substring(0, 3).toLowerCase() != "err")
            {
                data = Base64.decode(result);
                ldr = new Loader();
                ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function (event:Event)
            {
                var _loc_2:BitmapData = null;
                var _loc_3:Reform = null;
                if (ldr.content.width > 4000 || ldr.content.height > 4000)
                {
                    _loc_3 = new Reform();
                    _loc_2 = _loc_3.MaxSize(event.target.content.bitmapData, 4000, 4000);
                }
                else
                {
                    _loc_2 = event.target.content.bitmapData;
                }
                Paste(_loc_2);
                return;
            }// end function
            );
                ldr.loadBytes(data);
            }
            else
            {
                new AlertDialog(this.p, U.RM("rm_no_clipboard"));
            }
            return;
        }// end function

        public function Paste(type:BitmapData = null) : void
        {
            var _arg1:* = type;
            var topleft:*;
            var bm:* = _arg1;
            if (this.CheckCanvas())
            {
                if (bm == null && this.p.clipboard.bitmap != null)
                {
                    bm = this.p.clipboard.bitmap.clone();
                }
                if (bm == null)
                {
                    setTimeout(function ()
            {
                U.Clipboard(PasteGlobal);
                return;
            }// end function
            , 250);
                }
                else if (bm != null)
                {
                    this.DeselectAll(false);
                    topleft = new Point();
                    topleft.x = Math.round(this.p.canvas.clip.x + (this.p.canvas.clip.width - bm.width) / 2);
                    topleft.y = Math.round(this.p.canvas.clip.y + (this.p.canvas.clip.height - bm.height) / 2);
                    if (topleft.x < 0)
                    {
                        topleft.x = 0;
                    }
                    if (topleft.y < 0)
                    {
                        topleft.y = 0;
                    }
                    this.NewLayer(bm, topleft, false);
                    this.p.canvas.Message("rm_paste");
                }
            }
            return;
        }// end function

        public function Clear() : void
        {
            if (this.CheckCanvas() && this.p.canvas.layer.hasBitmap)
            {
                this.p.canvas.history.Prepare(Action.Clear, true);
                Misc.Clear(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.selection, this.p.canvas.layer.offset, Boolean(this.p.canvas.document.anr != 0 || this.p.canvas.document.transparent));
                this.p.layers.Update();
                this.p.canvas.Message("rm_clear");
            }
            return;
        }// end function

        private function Slice(OpenImageLibrary$0:Boolean) : BitmapData
        {
            var _loc_2:BitmapData = null;
            var _loc_3:Rectangle = null;
            if (this.CheckCanvas() && this.p.canvas.hasSelection && this.p.canvas.layer.hasBitmap)
            {
                if (OpenImageLibrary$0)
                {
                    this.p.canvas.history.Prepare(Action.Cut, true);
                }
                _loc_3 = Misc.GetAffectedRect(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.selection, this.p.canvas.layer.offset);
                if (_loc_3.width > 0 && _loc_3.height > 0)
                {
                    _loc_2 = new BitmapData(_loc_3.width, _loc_3.height, true, 16777215);
                    _loc_2.copyPixels(this.p.canvas.layer.bitmap.bitmapData, _loc_3, _loc_2.rect.topLeft);
                    Misc.ApplySelectionMask(_loc_2, this.p.canvas.selection, new Point(_loc_3.x + this.p.canvas.layer.offset.x, _loc_3.y + this.p.canvas.layer.offset.y));
                    if (OpenImageLibrary$0)
                    {
                        Misc.Clear(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.selection, this.p.canvas.layer.offset, Boolean(this.p.canvas.document.anr != 0 || this.p.canvas.document.transparent));
                    }
                }
                if (OpenImageLibrary$0)
                {
                    this.p.canvas.RemoveSelection();
                    this.p.layers.Update();
                }
            }
            return _loc_2;
        }// end function

        public function Crop() : void
        {
            if (this.CheckCanvas() && this.p.canvas.hasSelection)
            {
                this.p.canvas.CropCanvas(this.p.canvas.selection.rect);
            }
            return;
        }// end function

        public function SelectAll(... args) : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.SelectAll();
                this.p.canvas.Message("rm_select_all");
                this.p.navigator.SetSelectionSize(this.p.canvas.selection.width, this.p.canvas.selection.height);
                this.p.tool.Reset();
            }
            return;
        }// end function

        public function DeselectAll(type:Boolean = true) : void
        {
            if (this.CheckCanvas() && this.p.canvas.hasSelection)
            {
                this.p.canvas.DeselectAll();
                if (type)
                {
                    this.p.canvas.Message("rm_deselect_all");
                }
                this.p.navigator.SetSelectionSize(0, 0);
                this.p.tool.Reset();
            }
            return;
        }// end function

        public function InvertSelection(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.hasSelection)
            {
                this.p.canvas.InvertSelection();
                this.p.canvas.Message("rm_invert_selection");
                this.p.navigator.SetSelectionSize(this.p.canvas.selection.width, this.p.canvas.selection.height);
                this.p.tool.Reset();
            }
            return;
        }// end function

        public function DefineBrush() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ColorMatrix = null;
            var _loc_4:Rectangle = null;
            var _loc_5:uint = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Vector.<int> = null;
            var _loc_11:int = 0;
            var _loc_12:Brush = null;
            var _loc_13:int = 0;
            if (this.CheckCanvas() && this.p.canvas.hasSelection)
            {
                _loc_1 = this.Slice(false);
                if (_loc_1 != null)
                {
                    _loc_2 = new BitmapData(_loc_1.width, _loc_1.height, false, 16777215);
                    _loc_2.copyPixels(_loc_1, _loc_1.rect, _loc_1.rect.topLeft);
                    _loc_3 = new ColorMatrix();
                    _loc_3.Desaturate();
                    _loc_3.Invert();
                    _loc_2.applyFilter(_loc_2, _loc_2.rect, _loc_2.rect.topLeft, _loc_3.filter);
                    _loc_4 = _loc_2.getColorBoundsRect(16777215, 0, false);
                    if (_loc_4.width > 0 && _loc_4.height > 0)
                    {
                        _loc_9 = 0;
                        _loc_10 = new Vector.<int>;
                        _loc_11 = _loc_4.top;
                        while (_loc_11 < _loc_4.bottom)
                        {
                            
                            _loc_13 = _loc_4.left;
                            while (_loc_13 < _loc_4.right)
                            {
                                
                                _loc_5 = _loc_2.getPixel(_loc_13, _loc_11);
                                _loc_6 = _loc_5 >> 16 & 255;
                                _loc_7 = _loc_5 >> 8 & 255;
                                _loc_8 = _loc_5 & 255;
                                _loc_10[_loc_9] = Math.round((_loc_6 + _loc_7 + _loc_8) / 3);
                                _loc_9++;
                                _loc_13++;
                            }
                            _loc_11++;
                        }
                        _loc_12 = new Brush(BrushType.Custom, _loc_10, _loc_4.width, _loc_4.height, _loc_4.width / 4, null);
                        this.p.options.brushes.Add(_loc_12);
                        this.p.canvas.Message("rm_brush_defined");
                    }
                }
            }
            return;
        }// end function

        public function MakeBrushLarger(... args) : void
        {
            this.p.options.selected.bp.SetShapeProperty(190, true);
            if (AppState.OnCanvas)
            {
                this.p.cursor.Set(this.p.tool.type);
            }
            return;
        }// end function

        public function MakeBrushSmaller(... args) : void
        {
            this.p.options.selected.bp.SetShapeProperty(188, true);
            if (AppState.OnCanvas)
            {
                this.p.cursor.Set(this.p.tool.type);
            }
            return;
        }// end function

        public function MakeBrushHarder(... args) : void
        {
            this.p.options.selected.bp.SetShapeProperty(290, true);
            if (AppState.OnCanvas)
            {
                this.p.cursor.Set(this.p.tool.type);
            }
            return;
        }// end function

        public function MakeBrushSofter(... args) : void
        {
            this.p.options.selected.bp.SetShapeProperty(288, true);
            if (AppState.OnCanvas)
            {
                this.p.cursor.Set(this.p.tool.type);
            }
            return;
        }// end function

        public function ImageSize() : void
        {
            if (this.CheckCanvas())
            {
                new ImageSizeDialog(this.p);
            }
            return;
        }// end function

        public function CanvasSize() : void
        {
            if (this.CheckCanvas())
            {
                new CanvasSizeDialog(this.p);
            }
            return;
        }// end function

        public function FlipCanvasVertical() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.FlipImage(FlipType.Vertical);
                this.p.canvas.Message("rm_flip_canvas");
            }
            return;
        }// end function

        public function FlipCanvasHorizontal() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.FlipImage(FlipType.Horizontal);
                this.p.canvas.Message("rm_flip_canvas");
            }
            return;
        }// end function

        public function RotateCanvas90CW() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.RotateImage(90);
                this.p.canvas.Message("rm_rotate_canvas");
            }
            return;
        }// end function

        public function RotateCanvas180() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.RotateImage(180);
                this.p.canvas.Message("rm_rotate_canvas");
            }
            return;
        }// end function

        public function RotateCanvas90CCW() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.RotateImage(270);
                this.p.canvas.Message("rm_rotate_canvas");
            }
            return;
        }// end function

        public function NewEmptyLayer(... args)
        {
            this.NewLayer();
            return;
        }// end function

        public function NewLayer(type:BitmapData = null, type:Point = null, type:Boolean = true) : void
        {
            var _loc_4:int = 0;
            if (this.CheckCanvas())
            {
                _loc_4 = this.p.canvas.document.anr + 1;
                this.p.canvas.document.AddBitmapLayer(type, _loc_4, type);
                this.p.canvas.history.Prepare(Action.LayerAddBitmap, true);
                this.p.layers.Update();
                this.p.tool.Reset();
                if (type)
                {
                    this.p.canvas.Message("rm_new_layer");
                }
            }
            return;
        }// end function

        public function DuplicateLayer(... args) : void
        {
            args = 0;
            var _loc_3:Layer = null;
            if (this.CheckCanvas())
            {
                args = this.p.canvas.document.anr + 1;
                _loc_3 = this.p.canvas.layer.Clone();
                if (_loc_3.name.indexOf(U.RM("rm_small_copy")) == -1)
                {
                    _loc_3.name = _loc_3.name + (" " + U.RM("rm_small_copy"));
                }
                this.p.canvas.document.AddLayer(_loc_3, args);
                this.p.canvas.history.Prepare(Action.LayerDublicate, true);
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_duplicate_layer");
            }
            return;
        }// end function

        public function PasteLayer(type:Layer) : void
        {
            var _loc_2:int = 0;
            if (this.CheckCanvas())
            {
                _loc_2 = this.p.canvas.document.anr + 1;
                if (type.name.indexOf(U.RM("rm_small_copy")) == -1)
                {
                    type.name = type.name + (" " + U.RM("rm_small_copy"));
                }
                this.p.canvas.document.AddLayer(type, _loc_2);
                this.p.canvas.history.Prepare(Action.LayerPaste, true);
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_paste_layer");
            }
            return;
        }// end function

        public function DeleteLayer(... args) : void
        {
            if (this.CheckCanvas() && this.CheckNotLockedLayer())
            {
                if (this.p.canvas.document.numChildren > 1)
                {
                    this.p.canvas.history.Prepare(Action.LayerRemove, true);
                    this.p.canvas.document.RemoveLayer();
                    this.p.layers.Update();
                    this.p.tool.Reset();
                    this.p.canvas.Message("rm_delete_layer");
                }
            }
            return;
        }// end function

        public function ShowLayerStyle(... args) : void
        {
            if (this.CheckCanvas())
            {
                new LayerStyle(this.p);
            }
            return;
        }// end function

        public function CopyLayerStyle(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.layer.filters != null && this.p.canvas.layer.filters.length > 0)
            {
                this.p.clipboard.layerstyle = this.p.canvas.layer.CloneStyle();
            }
            return;
        }// end function

        public function PasteLayerStyle(... args) : void
        {
            args = null;
            var _loc_3:int = 0;
            if (this.CheckCanvas() && this.p.clipboard.layerstyle != null)
            {
                args = new Array();
                _loc_3 = 0;
                while (_loc_3 < this.p.clipboard.layerstyle.length)
                {
                    
                    args.push(this.p.clipboard.layerstyle[_loc_3]);
                    _loc_3++;
                }
                this.p.canvas.layer.filters = args;
                this.p.layers.Update();
            }
            return;
        }// end function

        public function ClearLayerStyle(... args) : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.layer.filters = null;
                this.p.layers.Update();
            }
            return;
        }// end function

        public function OrderLayerUp(... args) : void
        {
            if (this.CheckCanvas() && this.CheckNotLockedLayer())
            {
                if (this.p.canvas.document.anr != (this.p.canvas.document.numChildren - 1))
                {
                    this.OrderLayer((this.p.canvas.document.anr + 1));
                }
            }
            return;
        }// end function

        public function OrderLayerDown(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.document.anr != 0)
            {
                if (this.p.canvas.document.anr != 1 || this.p.canvas.document.transparent)
                {
                    this.OrderLayer((this.p.canvas.document.anr - 1));
                }
            }
            return;
        }// end function

        public function OrderLayer(type:int) : void
        {
            this.p.canvas.history.Did(new HistoryEntry(Action.LayerOrder, this.p.canvas.document.anr, type));
            this.p.canvas.document.LayerOrder(this.p.canvas.document.anr, type);
            this.p.layers.Update();
            this.p.tool.Reset();
            return;
        }// end function

        public function MergeLayerDown(... args) : void
        {
            this.MergeLayer();
            return;
        }// end function

        public function MergeVisibleLayers(... args) : void
        {
            this.MergeLayers(false);
            return;
        }// end function

        public function FlattenImage(... args) : void
        {
            this.MergeLayers(true);
            return;
        }// end function

        public function MergeLayers(type:Boolean) : void
        {
            var _loc_2:Layer = null;
            var _loc_3:BitmapData = null;
            var _loc_4:int = 0;
            if (this.CheckCanvas() && this.p.canvas.document.numChildren > 1)
            {
                this.p.canvas.history.Prepare(Action.LayerMerge, true);
                _loc_2 = this.p.canvas.document.GetLayer(0);
                if (_loc_2.type != LayerType.Bitmap)
                {
                    this.p.canvas.document.RasterizeLayer(0);
                }
                _loc_3 = new BitmapData(this.p.canvas.dw, this.p.canvas.dh, true, 16777215);
                _loc_3.draw(this.p.canvas.document);
                _loc_2.bitmap.bitmapData = _loc_3;
                _loc_2.x = 0;
                _loc_2.y = 0;
                _loc_4 = this.p.canvas.document.numChildren - 1;
                while (_loc_4 > 0)
                {
                    
                    _loc_2 = this.p.canvas.document.GetLayer(_loc_4);
                    if (_loc_2.visible != false || type)
                    {
                        this.p.canvas.document.RemoveLayer(_loc_4);
                    }
                    _loc_4 = _loc_4 - 1;
                }
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_layer_merge");
            }
            return;
        }// end function

        public function MergeLayer(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.document.anr != 0)
            {
                this.p.canvas.history.Prepare(Action.LayerMerge, true);
                this.p.canvas.document.MergeLayers(this.p.canvas.document.anr, (this.p.canvas.document.anr - 1));
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_layer_merge");
            }
            return;
        }// end function

        public function AddLayerMask(... args) : void
        {
            args = null;
            var _loc_3:BitmapData = null;
            var _loc_4:BitmapData = null;
            if (this.CheckBitmapLayer() && this.CheckNotLockedLayer() && !this.p.canvas.layer.hasMask)
            {
                this.p.canvas.history.Prepare(Action.LayerMaskAdd, true);
                this.p.canvas.layer.AddMask(null);
                if (this.p.canvas.hasSelection)
                {
                    args = this.p.canvas.selection.AlphaMask();
                    _loc_3 = new BitmapData(args.width, args.height, true, this.p.toolbar.GetColor(1, true));
                    _loc_4 = this.p.canvas.layer.SetBitmapEditMode(1, _loc_3.rect);
                    _loc_4.copyPixels(_loc_3, _loc_3.rect, _loc_3.rect.topLeft, args, args.rect.topLeft, true);
                    this.p.canvas.layer.RemoveBitmapEditMode();
                    this.p.canvas.DeselectAll();
                }
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_add_layer_mask");
            }
            return;
        }// end function

        public function DeleteLayerMask(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.layer.hasMask)
            {
                this.p.canvas.history.Prepare(Action.LayerMaskRemove, true);
                this.p.canvas.layer.RemoveMask();
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_delete_layer_mask");
            }
            return;
        }// end function

        public function ApplyLayerMask(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.layer.hasMask)
            {
                this.p.canvas.history.Prepare(Action.LayerMaskApply, true);
                this.p.canvas.layer.ApplyMask();
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_apply_layer_mask");
            }
            return;
        }// end function

        public function RasterizeType(... args) : void
        {
            if (this.CheckCanvas() && this.p.canvas.layer.type == LayerType.Text)
            {
                this.p.canvas.history.Prepare(Action.LayerRasterize, true);
                this.p.canvas.document.RasterizeLayer();
                this.p.layers.Update();
                this.p.tool.Reset();
                this.p.canvas.Message("rm_rasterize_layer");
            }
            return;
        }// end function

        public function FlipLayerVertical() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.FlipLayer(FlipType.Vertical);
                this.p.canvas.Message("rm_flip_layer");
            }
            return;
        }// end function

        public function FlipLayerHorizontal() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.FlipLayer(FlipType.Horizontal);
                this.p.canvas.Message("rm_flip_layer");
            }
            return;
        }// end function

        public function RotateLayer90CW() : void
        {
            if (this.CheckCanvas() && this.CheckNotLockedLayer())
            {
                this.p.canvas.RotateLayer(90);
                this.p.canvas.Message("rm_rotate_layer");
            }
            return;
        }// end function

        public function RotateLayer180() : void
        {
            if (this.CheckCanvas())
            {
                this.p.canvas.RotateLayer(180);
                this.p.canvas.Message("rm_rotate_layer");
            }
            return;
        }// end function

        public function RotateLayer90CCW() : void
        {
            if (this.CheckCanvas() && this.CheckNotLockedLayer())
            {
                this.p.canvas.RotateLayer(270);
                this.p.canvas.Message("rm_rotate_layer");
            }
            return;
        }// end function

        public function CropOverlay(type:Rectangle, type:Object) : void
        {
            if (this.CheckCanvas())
            {
                new CropTool(this.p, type, type);
            }
            return;
        }// end function

        public function FreeTransform(... args) : void
        {
            if (this.CheckValidBitmapLayer())
            {
                new TransformTool(this.p);
            }
            return;
        }// end function

        public function FreeDistort(... args) : void
        {
            if (this.CheckValidBitmapLayer())
            {
                new DistortTool(this.p);
            }
            return;
        }// end function

        public function BrightnessContrast() : void
        {
            if (this.CheckBitmapLayer())
            {
                new BrightnessContrastDialog(this.p);
            }
            return;
        }// end function

        public function HueSaturation() : void
        {
            if (this.CheckBitmapLayer())
            {
                new HueSaturationDialog(this.p);
            }
            return;
        }// end function

        public function Levels() : void
        {
            if (this.CheckBitmapLayer())
            {
                new LevelsDialog(this.p);
            }
            return;
        }// end function

        public function Curves() : void
        {
            if (this.CheckBitmapLayer())
            {
                new CurvesDialog(this.p);
            }
            return;
        }// end function

        public function Exposure() : void
        {
            if (this.CheckBitmapLayer())
            {
                new ExposureDialog(this.p);
            }
            return;
        }// end function

        public function Posterize() : void
        {
            if (this.CheckBitmapLayer())
            {
                new PosterizeDialog(this.p);
            }
            return;
        }// end function

        public function Threshold() : void
        {
            if (this.CheckBitmapLayer())
            {
                new ThresholdDialog(this.p);
            }
            return;
        }// end function

        public function AutoLevels() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.AutoLevelsAdjustment, true);
                this.f.AutoLevels(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_auto_levels");
            }
            return;
        }// end function

        public function Invert() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ColorMatrix = null;
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.InvertAdjustment, true);
                _loc_1 = this.p.canvas.layer.bitmap.bitmapData;
                _loc_2 = _loc_1.clone();
                _loc_3 = new ColorMatrix();
                _loc_3.Invert();
                _loc_1.applyFilter(_loc_1, _loc_1.rect, _loc_1.rect.topLeft, _loc_3.filter);
                Misc.ApplySelection(_loc_1, _loc_2, this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_invert");
            }
            return;
        }// end function

        public function Desaturate() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ColorMatrix = null;
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.DesaturateAdjustment, true);
                _loc_1 = this.p.canvas.layer.bitmap.bitmapData;
                _loc_2 = _loc_1.clone();
                _loc_3 = new ColorMatrix();
                _loc_3.Desaturate();
                _loc_1.applyFilter(_loc_1, _loc_1.rect, _loc_1.rect.topLeft, _loc_3.filter);
                Misc.ApplySelection(_loc_1, _loc_2, this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_desaturate");
            }
            return;
        }// end function

        public function Sepia() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.SepiaAdjustment, true);
                this.f.Sepia(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_sepia");
            }
            return;
        }// end function

        public function OldPhoto() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.OldPhotoAdjustment, true);
                this.f.OldPhoto(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_old_photo");
            }
            return;
        }// end function

        public function Hope() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.HopeFilter, true);
                this.f.Hope(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_hope");
            }
            return;
        }// end function

        public function CrossProcess() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.CrossProcessFilter, true);
                this.f.CrossProcess(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_cross_process");
            }
            return;
        }// end function

        public function Solarize() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.SolarizeFilter, true);
                this.f.Solarize(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_solarize");
            }
            return;
        }// end function

        public function GlamourGlow() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.GlamourGlowFilter, true);
                this.f.GlamourGlow(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_glamour_glow");
            }
            return;
        }// end function

        public function MimicHDR() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.MimicHDRFilter, true);
                this.f.MimicHDR(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_mimic_hdr");
            }
            return;
        }// end function

        public function Sharpen() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.SharpenFilter, true);
                this.f.Sharpen(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_sharpen");
            }
            return;
        }// end function

        public function Blur() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.BlurFilter, true);
                this.f.Blur(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_blur");
            }
            return;
        }// end function

        public function Denoise() : void
        {
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.DenoiseFilter, true);
                this.f.Denoise(this.p.canvas.layer.bitmap.bitmapData, this.p.canvas.layer.bitmap.bitmapData.clone(), this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_denoise");
            }
            return;
        }// end function

        public function Emboss() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ConvolutionMatrix = null;
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.EmbossFilter, true);
                _loc_1 = this.p.canvas.layer.bitmap.bitmapData;
                _loc_2 = _loc_1.clone();
                _loc_3 = new ConvolutionMatrix();
                _loc_3.Emboss();
                _loc_1.applyFilter(_loc_2, _loc_2.rect, _loc_2.rect.topLeft, _loc_3.filter);
                Misc.ApplySelection(_loc_1, _loc_2, this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_emboss");
            }
            return;
        }// end function

        public function Engrave() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ConvolutionMatrix = null;
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.EngraveAdjustment, true);
                _loc_1 = this.p.canvas.layer.bitmap.bitmapData;
                _loc_2 = _loc_1.clone();
                _loc_3 = new ConvolutionMatrix();
                _loc_3.Engrave();
                _loc_1.applyFilter(_loc_2, _loc_2.rect, _loc_2.rect.topLeft, _loc_3.filter);
                Misc.ApplySelection(_loc_1, _loc_2, this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_engrave");
            }
            return;
        }// end function

        public function FindEdges() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:BitmapData = null;
            var _loc_3:ConvolutionMatrix = null;
            if (this.CheckValidBitmapLayer())
            {
                this.p.canvas.history.Prepare(Action.FindEdgesFilter, true);
                _loc_1 = this.p.canvas.layer.bitmap.bitmapData;
                _loc_2 = _loc_1.clone();
                _loc_3 = new ConvolutionMatrix();
                _loc_3.FindEdges();
                _loc_1.applyFilter(_loc_2, _loc_2.rect, _loc_2.rect.topLeft, _loc_3.filter);
                Misc.ApplySelection(_loc_1, _loc_2, this.p.canvas.selection, this.p.canvas.layer.offset);
                this.p.canvas.UpdateAfterEdit();
                this.p.canvas.Message("rm_find_edges");
            }
            return;
        }// end function

        public function Halftone() : void
        {
            if (this.CheckBitmapLayer())
            {
                new HalftoneDialog(this.p);
            }
            return;
        }// end function

        public function BoxBlur() : void
        {
            if (this.CheckBitmapLayer())
            {
                new BoxBlurDialog(this.p);
            }
            return;
        }// end function

        public function UnsharpMask() : void
        {
            if (this.CheckBitmapLayer())
            {
                new UnsharpMaskDialog(this.p);
            }
            return;
        }// end function

        public function Pixelate() : void
        {
            if (this.CheckBitmapLayer())
            {
                new PixelateDialog(this.p);
            }
            return;
        }// end function

        public function Pointinize() : void
        {
            if (this.CheckBitmapLayer())
            {
                new PointinizeDialog(this.p);
            }
            return;
        }// end function

        public function GaussianBlur() : void
        {
            if (this.CheckBitmapLayer())
            {
                new GaussianBlurDialog(this.p);
            }
            return;
        }// end function

        public function Noise() : void
        {
            if (this.CheckBitmapLayer())
            {
                new NoiseDialog(this.p);
            }
            return;
        }// end function

        public function Diffuse() : void
        {
            if (this.CheckBitmapLayer())
            {
                new DiffuseDialog(this.p);
            }
            return;
        }// end function

        public function Scanlines() : void
        {
            if (this.CheckBitmapLayer())
            {
                new ScanlineDialog(this.p);
            }
            return;
        }// end function

        public function Waterswirl() : void
        {
            if (this.CheckBitmapLayer())
            {
                new WaterswirlDialog(this.p);
            }
            return;
        }// end function

        public function Kaleidoscope() : void
        {
            if (this.CheckBitmapLayer())
            {
                new KaleidoscopeDialog(this.p);
            }
            return;
        }// end function

        public function PolarCoordinates() : void
        {
            if (this.CheckBitmapLayer())
            {
                new PolarCoordinatesDialog(this.p);
            }
            return;
        }// end function

        public function Pastel() : void
        {
            if (this.CheckBitmapLayer())
            {
                new PastelDialog(this.p);
            }
            return;
        }// end function

        public function ArtPoster() : void
        {
            if (this.CheckBitmapLayer())
            {
                new ArtPosterDialog(this.p);
            }
            return;
        }// end function

        public function Heatmap() : void
        {
            if (this.CheckBitmapLayer())
            {
                new HeatmapDialog(this.p);
            }
            return;
        }// end function

        public function TriTone() : void
        {
            if (this.CheckBitmapLayer())
            {
                new TriToneDialog(this.p);
            }
            return;
        }// end function

        public function ColorLookup() : void
        {
            if (this.CheckBitmapLayer())
            {
                new ColorLookupDialog(this.p);
            }
            return;
        }// end function

        public function Vignette()
        {
            if (this.CheckBitmapLayer())
            {
                new VignetteDialog(this.p);
            }
            return;
        }// end function

        public function TiltShift() : void
        {
            if (this.CheckBitmapLayer())
            {
                new TiltShiftDialog(this.p);
            }
            return;
        }// end function

        public function ZoomIn(... args) : void
        {
            if (this.CheckCanvas())
            {
                this.p.navigator.ZoomPlus();
            }
            return;
        }// end function

        public function ZoomOut(... args) : void
        {
            if (this.CheckCanvas())
            {
                this.p.navigator.ZoomMinus();
            }
            return;
        }// end function

        public function ZoomActualPixels(... args) : void
        {
            if (this.CheckCanvas())
            {
                this.p.navigator.SetZoom(100, true);
                this.p.canvas.Message("rm_actual_pixels");
            }
            return;
        }// end function

        public function ZoomShowAll(type:Boolean = false) : void
        {
            var _loc_2:int = 0;
            if (this.CheckCanvas())
            {
                _loc_2 = this.p.canvas.canvasheight / this.p.canvas.dh > this.p.canvas.canvaswidth / this.p.canvas.dw ? (Math.round(this.p.canvas.canvaswidth / this.p.canvas.dw * 100)) : (Math.round(this.p.canvas.canvasheight / this.p.canvas.dh * 100));
                if (type && _loc_2 > 100)
                {
                    _loc_2 = 100;
                }
                this.p.navigator.SetZoom(_loc_2, true);
                this.p.canvas.Message("rm_show_all");
            }
            return;
        }// end function

        public function ShowHideNavigator() : void
        {
            this.p.navigator.visible = !this.p.navigator.visible;
            if (this.p.navigator.visible)
            {
                this.p.BringToTop(this.p.navigator);
            }
            return;
        }// end function

        public function ShowAd() : void
        {
            this.p.ad.visible = !this.p.ad.visible;
            if (this.p.ad.visible)
            {
                this.p.BringToTop(this.p.ad);
            }
            return;
        }// end function

        public function ShowHideLayers() : void
        {
            this.p.layers.visible = !this.p.layers.visible;
            if (this.p.layers.visible)
            {
                this.p.BringToTop(this.p.layers);
            }
            return;
        }// end function

        public function ShowHideHistory() : void
        {
            this.p.history.visible = !this.p.history.visible;
            if (this.p.history.visible)
            {
                this.p.BringToTop(this.p.history);
            }
            return;
        }// end function

        public function ShowHideOptions() : void
        {
            if (!AppState.Maximized)
            {
                this.p.options.visible = !this.p.options.visible;
            }
            return;
        }// end function

        public function ResetPaletteLocation() : void
        {
            this.p.ResetPaletteLocation();
            return;
        }// end function

        public function ToggleFullScreenMode(event:MouseEvent) : void
        {
            this.p.stage.displayState = this.p.stage.displayState == "normal" ? ("fullScreen") : ("normal");
            this.p.ResetPaletteLocation();
            return;
        }// end function

        public function LoginUser(type:Object) : void
        {
            this.p.user = type;
            this.p.menubar.SetUserDetails();
            this.p.menubar.file.ChangeItemVisibility("rm_login", false);
            this.p.menubar.file.ChangeItemVisibility("rm_register", false);
            this.p.menubar.file.ChangeItemVisibility("rm_logout", true);
            return;
        }// end function

        public function LogoutUser(... args) : void
        {
            if (this.p.user != null)
            {
                U.CallHome("logout", {doit:"yes"}, U.TrashBack);
            }
            this.p.user = null;
            this.p.menubar.SetUserDetails();
            this.p.menubar.file.ChangeItemVisibility("rm_login", true);
            this.p.menubar.file.ChangeItemVisibility("rm_register", true);
            this.p.menubar.file.ChangeItemVisibility("rm_logout", false);
            return;
        }// end function

        public function Exit() : void
        {
            navigateToURL(new URLRequest(U.IsParam("exit") ? (U.Param("exit")) : ("/")), "_self");
            return;
        }// end function

        public function OpenURL(type:String) : void
        {
            navigateToURL(new URLRequest(type), "_blank");
            return;
        }// end function

    }
}
